<?php $this->_extends('_layouts/main_layout'); ?>
<?php $this->_block('title'); ?>
	Add <?php echo isset($pub) ? "Publisher" : "Admin"; ?>
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>
<div id="content" class="box">
	<h1>Add <?php echo isset($pub) ? "Publisher" : "Admin"; ?></h1>
		<?php if (isset($infos)) 
				{
					foreach ($infos as $info)
					{
		?>
				<p class="msg info"><?php echo $info;?></p>
		<?php		}
				}
		?>
		<fieldset>
			<legend>Add <?php echo isset($pub) ? "Publisher" : "Admin"; ?></legend>
		<?php if (isset($errors)) 
				{
					foreach ($errors as $error)
					{
		?>
				<p class="msg error"><?php echo $error;?></p>
		<?php		}
				}
		?>
		<form method="post" action="<?php echo url('admin/add');?>">
		<table class="nostyle">
			<tr>
				<td>Email:</td>
				<td><input type="text" class="input-text" name="email" value="<?php if (isset($email)) echo $email;?>"/></td>
			</tr>
			<tr>
				<td>Name:</td>
				<td><input type="text" class="input-text" name="name" value="<?php if (isset($name)) echo $name;?>"/></td>
			</tr>
			<tr>
				<td>Company:</td>
				<td><input type="text" class="input-text" name="company" value="<?php if (isset($company)) echo $company;?>"/></td>
			</tr>
			<tr>
				<td>Phone:</td>
				<td><input type="text" class="input-text" name="phone" value="<?php if (isset($phone)) echo $phone;?>"/></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="password" class="input-text" name="password" value=""/></td>
			</tr>
			<tr>
				<td>Confirm Password:</td>
				<td><input type="password" class="input-text" name="password2" value=""/></td>
			</tr>
			<?php if(!isset($pub)): ?>
			<tr id="tr_role">
				<td>Role:</td>
				<td>
					<select name="role" id="role">
						<option value="">--Choose a Role--</option>
						<option value="ADMIN" <?php echo ('ADMIN' == $role) ? 'selected="selected"' : '';?>>System Admin</option>
						<option value="GAMECO" <?php echo ('GAMECO' == $role) ?  'selected="selected"' : ''; ?>>CGB Manager</option>
						<option value="MARKETING" <?php echo ('MARKETING' == $role) ?  'selected="selected"' : ''; ?>>Media Buy Manager</option>
						<option value="READONLY" <?php echo ('READONLY' == $role) ?  'selected="selected"' : ''; ?>>Read Only</option>
					</select>
				</td>
			</tr>
			<?php else: ?>
			<tr>
				<td>Publisher:</td>
				<td>
					<select name="publisher" id="publisher">
						<option value="">--Choose a Publisher--</option>
						<?php 
							if(!empty($publishers)): 
								foreach ($publishers as $publisher):
						?>
						<option value="<?php echo $publisher['id']; ?>"><?php echo $publisher['name'];?></option>
						<?php
							 endforeach;
							 endif;
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>Sub:</td>
				<td>
					<select name="sub" id="sub">
						<option value="" id="sub_choose">--Choose a Sub--</option>
					</select>
				</td>
			</tr>
			
			<?php endif;?>
			<?php if(empty($publishers)&&isset($pub)): ?>
			<tr>
				<td colspan="2"><a href="<?php echo url('publisher/add'); ?> ">There's no publisher yet, click here to add new publisher</a></td>
			</tr>
			<?php endif;?>
			
			<tr>
				<td colspan="2" class="t-right"><input type="submit" class="input-submit" value="Save"/></td>
			</tr>
		</table>
		<?php if(isset($pub)): ?>
			<input type="hidden" name="role" value="PUBLISHER" />
			<input type="hidden" name="pub" value="1" />
		<?php endif;?>
		</form>
	</fieldset>
</div>
<?php if(isset($pub)): ?>
<script type="text/javascript">
	$(function(){
		$('#publisher').change(function(){
			var pid = $('#publisher').val();
			if ('' != pid){
				$.get('<?php echo url('admin/loadsubs'); ?>', {id: pid}, function(data){
					$('#sub').children(':gt(0)').remove();
					if('' == data){
						return;
					}
					var subs = eval('(' + data + ')')
					var sub = '';
					for(var i=0; i<subs.length; i++){
						sub += '<option value="'+subs[i]['id']+'">'+subs[i]['name']+'</option>';
					}
					$('#sub_choose').after(sub);
				});
			}
		});
	});
</script>
<?php endif;?>
<?php $this->_endblock(); ?>
